//
//  CropperImageViewController.m
//  WULIHH
//
//  Created by chen tang on 16/4/16.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "CropperImageViewController.h"
#import "Header.h"


#import "TWImageScrollView.h"
#import "CroperView.h"


#define widthPX         WithView
#define heightPX        heightView

//#define insertWidth     WithView-20

@interface CropperImageViewController ()<UIScrollViewDelegate>
{
    UIScrollView *mainScrollView;
    UIImageView *backImageView;
    CroperView *croperView;
    
    UIImageView *imageView1;
    UIImageView *imageView2;
    UIImageView *imageView3;
    
    UILabel *label1;
    UILabel *label2;
    UILabel *label3;
    
    int imageheight;
    int imagewidth;
    
    float scaleZoom;
    
}
@property (nonatomic, strong) UIImageView   *sourceIMageView;


//@property (strong, nonatomic) TWImageScrollView *imageScrollView;



@end

@implementation CropperImageViewController


- (void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:uicolor(214, 214, 214)];
    
    
    
    mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:mainScrollView];
    [mainScrollView setBounces:NO];
    
    backImageView = [[UIImageView alloc] init];
    [mainScrollView addSubview:backImageView];
    
    
//    self.imageScrollView = [[TWImageScrollView alloc] initWithFrame:CGRectMake(0, 40, WithView, WithView)];
//    [self.view addSubview:self.imageScrollView];
    croperView  = [[CroperView alloc] initWithFrame:CGRectMake(0, 00, WithView, WithView*4/3.0)];
    [croperView displayImage:self.sourceImage];
    [croperView setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:croperView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [mainScrollView setDelegate:self];
    
    imageheight = _sourceImage.size.height;
    imagewidth = _sourceImage.size.width;
    
    
    [croperView displayImage:_sourceImage];
    

//    [self.imageScrollView displayImage:_sourceImage];
    UIImage *image = nil;
    UIView *centerbackground = [[UIView alloc] initWithFrame:CGRectMake(0, heightPX - 140, widthPX, 100)];
    [centerbackground setBackgroundColor:uicolor(236, 236, 236)];
    
    
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, widthPX / 3.0, 100)];
//    [btn1 setBackgroundColor:[UIColor brownColor]];
//    [btn1 setTitle:@"正方形" forState:UIControlStateNormal];
    
    [btn1 setBackgroundImage:image forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(event1:) forControlEvents:UIControlEventTouchUpInside];
    [centerbackground addSubview:btn1];
    
    imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(btn1.bounds.size.width / 2.0 - 15, 25, 30, 30)];
    image = [UIImage imageNamed:@"xingzhuan1"];
    [imageView1 setImage:image];
    [btn1 addSubview:imageView1];
    
    label1 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(imageView1.frame)-5, CGRectGetMaxY(imageView1.frame)+15, 44, 25)];
    label1.text = @"正方形";
    [label1 setFont:[UIFont systemFontOfSize:14.0]];
    [label1 setTextColor:uicolor(157, 157, 157)];
    [btn1 addSubview:label1];
    
    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake( CGRectGetMaxX(btn1.frame), 0, widthPX / 3.0, 100)];
//    [btn2 setBackgroundColor:[UIColor whiteColor]];
//    [btn2 setTitle:@"3:4" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(event2:) forControlEvents:UIControlEventTouchUpInside];
    [centerbackground addSubview:btn2];
    
    imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(btn2.bounds.size.width / 2.0 - 15, 22, 30, 36)];
    image = [UIImage imageNamed:@"xingzhuan22"];
    [imageView2 setImage:image];
    [btn2 addSubview:imageView2];
    
    label2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(imageView2.frame), CGRectGetMaxY(imageView2.frame)+15, 44, 25)];
    label2.text = @"3 : 4";
    [label2 setFont:[UIFont systemFontOfSize:14.0]];
    [label2 setTextColor:[UIColor blackColor]];
    [btn2 addSubview:label2];
    
    UIButton *btn3 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(btn2.frame), 0, widthPX / 3.0,100)];
//    [btn3 setBackgroundColor:[UIColor blueColor]];
    [centerbackground addSubview:btn3];
    [btn3 addTarget:self action:@selector(event3:) forControlEvents:UIControlEventTouchUpInside];
    
    imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(btn3.bounds.size.width / 2.0 - 18, 26, 32, 23)];
    image = [UIImage imageNamed:@"xingzhuan3"];
    [imageView3 setImage:image];
    [btn3 addSubview:imageView3];
    
    label3 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(imageView2.frame), CGRectGetMaxY(imageView2.frame)+15, 44, 25)];
    label3.text = @"4 : 3";
    [label3 setFont:[UIFont systemFontOfSize:14.0]];
    [label3 setTextColor:uicolor(157, 157, 157)];
    [btn3 addSubview:label3];
    [self.view addSubview:centerbackground];
    
    
    UIButton *backupBtn = [[ UIButton alloc] initWithFrame:CGRectMake(0, heightView-40, WithView/2.0, 40)];
    [backupBtn setBackgroundColor:uicolor(249, 249, 249)];
    [backupBtn addTarget:self action:@selector(backupEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backupBtn];
    
    UIImageView *backimage = [[UIImageView alloc] initWithFrame:CGRectMake(85, 10, 20, 20)];
    image = [UIImage imageNamed:@"back"];
    [backimage setImage:image];
    [backupBtn addSubview:backimage];
    
    UIButton *determineBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(backupBtn.frame), heightView-40, WithView/2.0, 40)];
    [determineBtn setBackgroundColor:uicolor(249, 249, 249)];
    [determineBtn addTarget:self action:@selector(determineEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:determineBtn];
    
    UIImageView *determineimage = [[UIImageView alloc] initWithFrame:CGRectMake(85, 10, 28, 20)];
    image = [UIImage imageNamed:@"determine"];
    [determineimage setImage:image];
    [determineBtn addSubview:determineimage];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)event1:(id)btn{
    
    //    [croperView setFrame:CGRectMake(0, 27, 320, 320)];
    [croperView removeFromSuperview];
    croperView  = [[CroperView alloc] initWithFrame:CGRectMake(0, 60, WithView, WithView)];
    [croperView displayImage:self.sourceImage];
    [self.view addSubview:croperView];
    
    [imageView1 setImage:[UIImage imageNamed:@"xingzhuan11"]];
    [label1 setTextColor:uicolor(19, 19, 19)];
    [imageView2 setImage:[UIImage imageNamed:@"xingzhuan2"]];
    [label2 setTextColor:uicolor(157, 157, 157)];
    [imageView3 setImage:[UIImage imageNamed:@"xingzhuan3"]];
    [label3 setTextColor:uicolor(157, 157, 157)];
    
    
}
- (void)event2:(id)btn{
    
    //    NSLog(@"%f", biliheight(427.0));
    [croperView removeFromSuperview];
    croperView  = [[CroperView alloc] initWithFrame:CGRectMake(0, 0, WithView, WithView*4/3.0)];
    [croperView displayImage:self.sourceImage];
    [self.view addSubview:croperView];
    
    [imageView1 setImage:[UIImage imageNamed:@"xingzhuan1"]];
    [label1 setTextColor:uicolor(157, 157, 157)];
    [imageView2 setImage:[UIImage imageNamed:@"xingzhuan22"]];
    [label2 setTextColor:uicolor(19, 19, 19)];
    [imageView3 setImage:[UIImage imageNamed:@"xingzhuan3"]];
    [label3 setTextColor:uicolor(157, 157, 157)];
}
- (void)event3:(id)btn{
    
    //    [croperView setFrame:CGRectMake(0, 27, 320, 240)];
    [croperView removeFromSuperview];
    croperView  = [[CroperView alloc] initWithFrame:CGRectMake(0, 60, WithView, biliwith(240.0))];
    [croperView displayImage:self.sourceImage];
    [self.view addSubview:croperView];
    
    
    [imageView1 setImage:[UIImage imageNamed:@"xingzhuan1"]];
    [label1 setTextColor:uicolor(157, 157, 157)];
    [imageView2 setImage:[UIImage imageNamed:@"xingzhuan2"]];
    [label2 setTextColor:uicolor(157, 157, 157)];
    [imageView3 setImage:[UIImage imageNamed:@"xingzhuan33"]];
    [label3 setTextColor:uicolor(19, 19, 19)];
}




- (void)backupEvent:(id)btn{
    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"bubian" object:nil];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)determineEvent:(id)btn{
    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"bubian" object:nil];
    
    _height = croperView.bounds.size.height;
    _width = croperView.bounds.size.width;
    
    [self dismissViewControllerAnimated:YES completion:nil];
    //    UIImageWriteToSavedPhotosAlbum([croperView capture], self, NULL, NULL);
    if (_MCropperImageViewControllerDelegate && [_MCropperImageViewControllerDelegate respondsToSelector:@selector(croperWithImage:withRect:)]) {
        [_MCropperImageViewControllerDelegate croperWithImage:[croperView capture] withRect:CGRectMake(0, 0, _width, _height)];
    }

    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"%f", scrollView.bounds.size.height);
}

//- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
//{
//    NSLog(@"%f", scrollView.bounds.size.width);
//    return backImageView;
//}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
